package firstStage.Test;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @ClassName: [StudentManager]
 * @Description: 学生管理类
 * @Author: [黄劲松]
 * @CreateDate: [2021-02-07]
 * @Version: [v1.0]
 */
public class StudentManager {
    public static void main(String[] args) {
        //创建集合用于存贮学生数据
        ArrayList<Student> arrayList = new ArrayList<Student>();
        while (true) {
            System.out.println("========欢迎来到学生管理系统========");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看所有学生");
            System.out.println("5 退出");
            System.out.println("请输入你的选择：");

            Scanner sc = new Scanner(System.in);
            String line = sc.nextLine();
            switch (line) {
                case "1":
//                    System.out.println("添加学生");
                    addStudent(arrayList);
                    break;
                case "2":
//                    System.out.println("删除学生");
                    deleteStudent(arrayList);
                    break;
                case "3":
//                    System.out.println("修改学生");
                    updateStudent(arrayList);
                    break;
                case "4":
//                    System.out.println("查看所有学生");
                    queryStudent(arrayList);
                    break;
                case "5":
                    System.out.println("谢谢使用");
                    System.exit(0);//JVM退出
            }
        }
    }

    //添加学生信息
    /*public static void addStudent(ArrayList<Student> arrayList) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生学号：");
        String sid = sc.nextLine();
        System.out.println("请输入学生姓名：");
        String name = sc.nextLine();
        System.out.println("请输入学生年龄：");
        String age = sc.nextLine();
        System.out.println("请输入学生地址：");
        String address = sc.nextLine();

        Student s = new Student();
        s.setSid(sid);
        s.setName(name);
        s.setAge(age);
        s.setAddress(address);

        arrayList.add(s);
        System.out.println("添加学生成功");
    }*/
    public static void addStudent(ArrayList<Student> arrayList) {
        Scanner sc = new Scanner(System.in);
        String sid;
        while (true) {
            System.out.println("请输入学生学号：");
            sid = sc.nextLine();
            boolean flag = isUsed(arrayList, sid);
            if (flag) {
                System.out.println("你输入的学号已经被使用，请重新使用");
            } else {
                break;
            }
        }
        System.out.println("请输入学生姓名：");
        String name = sc.nextLine();
        System.out.println("请输入学生年龄：");
        String age = sc.nextLine();
        System.out.println("请输入学生地址：");
        String address = sc.nextLine();

        Student s = new Student();
        s.setSid(sid);
        s.setName(name);
        s.setAge(age);
        s.setAddress(address);

        arrayList.add(s);
        System.out.println("添加学生成功");
    }

    //查看学生信息
    public static void queryStudent(ArrayList<Student> arrayList) {
        if (arrayList.size() == 0) {
            System.out.println("无信息，请添加再查询");
            return;//为了让程序不在往下执行
        } else {
            System.out.println("学号\t姓名\t年龄\t居住地");
            for (int i = 0; i < arrayList.size(); i++) {
                Student s = arrayList.get(i);
                System.out.println(s.getSid() + "\t\t" + s.getName() + "\t\t" + s.getAge() + "\t\t" + s.getAddress());
            }
        }
    }

    //删除学生信息
    /*public static void deleteStudent(ArrayList<Student> arrayList){
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入你要删除的学生学号：");
        String sid=sc.nextLine();
        for (int i=0;i<arrayList.size();i++){
            Student s=arrayList.get(i);
            if (s.getSid().equals(sid)){
                arrayList.remove(i);
                break;
            }
        }
        System.out.println("删除学生成功");
    }*/
    public static void deleteStudent(ArrayList<Student> arrayList) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要删除的学生学号：");
        String sid = sc.nextLine();
        int index = -1;
        for (int i = 0; i < arrayList.size(); i++) {
            Student s = arrayList.get(i);
            if (s.getSid().equals(sid)) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            System.out.println("该信息不存在，请重新输入");
        } else {
            arrayList.remove(index);
            System.out.println("删除学生成功");
        }
    }

    //修改学生信息
    public static void updateStudent(ArrayList<Student> arrayList) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入修改学生学号：");
        String sid = sc.nextLine();

        System.out.println("请输入学生新姓名：");
        String name = sc.nextLine();
        System.out.println("请输入学生新年龄：");
        String age = sc.nextLine();
        System.out.println("请输入学生新地址：");
        String address = sc.nextLine();

        Student s = new Student();
        s.setSid(sid);
        s.setName(name);
        s.setAge(age);
        s.setAddress(address);
        for (int i = 0; i < arrayList.size(); i++) {
            Student student = arrayList.get(i);
            if (student.getSid().equals(sid)) {
                arrayList.set(i, s);
                break;
            }
        }
        arrayList.add(s);
        System.out.println("学生信息修改成功");
    }

    //判断学号是否已经被使用
    public static boolean isUsed(ArrayList<Student> arrayList, String sid) {
        boolean flag = false;
        for (int i = 0; i < arrayList.size(); i++) {
            Student s = arrayList.get(i);
            if (s.getSid().equals(sid)) {
                flag = true;
                break;
            }
        }
        return flag;
    }

}
